﻿Public Class Frm_Manajemen_User_add

    Private Sub Frm_Manajemen_User_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Call InisialisasiAplikasi()
        'Call openConnection()
    End Sub

    Private Sub Frm_Manajemen_User_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_nim.Focus()
    End Sub

    Private Sub cmd_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_save.Click

        Validation.clearRules()
        Validation.addRules(txt_nim.Text, "NIM", "required|length[7-7]")
        Validation.addRules(txt_username.Text, "username", "required|username|length[3-10]")
        Validation.addRules(txt_password.Text, "password", "required|password|length[5-32]")

        '# cek validasi
        If Validation.isValid Then
            Db.FlushCache()
            Db.Selects("*")
            Db.From("tbl_user")
            Db.Where("user", txt_username.Text)

            Connection.ExecuteToDataReader(Db.GetQueryString)

            If Connection.Read.HasRows Then
                MsgBox("Sudah terdapat username pada database !" & vbCrLf & "Ganti dengan yang lain.", MsgBoxStyle.OkOnly, "Informasi")
                Exit Sub
            End If

            'Db.FlushCache()
            'Db.Insert("tbl_user")
            'Db.SetField("user", txt_username.Text)
            'Db.SetField("pass", txt_password.Text)
            'Db.SetField("nim", txt_nim.Text)
            'Db.SetField("type", cmb_type.Text)

            Dim query As String = vbNullString
            query &= "INSERT INTO tbl_user (user, pass, nim, type) VALUES ("
            query &= " '" & txt_username.Text & "', MD5('" & txt_password.Text & "'), '" & txt_nim.Text & "', '" & cmb_type.SelectedIndex & "' "
            query &= " )"

            If Connection.ExecuteNonQuery(query) Then ' jika eksekusi queri berhasil
                MsgBox("Berhasil Disimpan", MsgBoxStyle.Information)
                txt_nim.Text = ""
                txt_username.Text = ""
                txt_password.Text = ""
                cmb_type.Text = "- Pilih salah satu -"

                frm_user_list.InitGrid()
                Me.Close()
            Else
                MsgBox("Tidak Berhasil DISIMPAN", MsgBoxStyle.Information)
            End If
        Else
            Validation.showMessage()
        End If

    End Sub

    Private Sub cmd_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_cancel.Click
        txt_nim.Text = ""
        txt_username.Text = ""
        txt_password.Text = ""
        cmb_type.Text = "- Pilih salah satu -"
        Me.Close()

    End Sub
End Class